/**
 * Created by shudong.msd(风骐) on 2016/4/17.
 */


app.directive('cancelConfirm',function(){

    var comutil = new commUtils();

    function syncData(scope,targetId){
        for(var i in scope.proceeMission.list){
            if(scope.proceeMission.list[i].id==targetId){
                scope.proceeMission.list.splice(i,1);
                scope.proceeMission.count -=1;
                scope.proceeMission.currentRecords -=1;
                break;
            }
        }

    }

    return{
        restrict:'A',
        controller:function($scope,$http,$state){
            $scope.cancelMission = function(targetId){
                $http.post(UrlMappings.CANCEL_MISSION+targetId,null,{
                    params : {
                        'access_token':comutil.getTocken()
                    },
                    headers:{
                        'Accept':'application/json',
                        'Content-Type':'application/x-www-form-urlencoded'
                    }
                }).success(function(data){
                    if(!data){
                        alert('取消失败');
                    }
                }).error(function(data,status){
                    if(status=='401')comutil.logout($state);
                });
            }
        },
        link:function(scope,ele,attr){
            ele.confirm({
                text: "你确定要取消此查询任务吗？",
                confirm: function(button) {
                    var targetId = attr.cancelConfirm;
                    //后台接口?id=targetId
                    scope.cancelMission(targetId);

                    scope.$apply(syncData(scope,targetId));

                },
                cancel: function(button) {
                    // nothing to do
                },
                confirmButton: "确定",
                cancelButton: "取消",
                post: false,
                confirmButtonClass: "mz-ok-btn",
                cancelButtonClass: "mz-no-btn",
                dialogClass: "mz-dialog"
            });
        }
    };
});
